#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "uartclass.h"


uartClass uartModule;
int Protocol_n = -1;

MainWindow* tmpClass;

extern "C" int Dispatch(bProtoID_t id, uint8_t cmd, uint8_t *param, bProtoLen_t param_len);


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    quartTimer = new QTimer(this);
    quartTimer->setSingleShot(true);
    connect(quartTimer, SIGNAL(timeout()), this, SLOT(TimerTimeout()));
    quartTimer->start(100);
    ui->comboBox->addItems(uartModule.uartComAvailable);

    Protocol_n = bProtocolRegist(0x1314, Dispatch);
    tmpClass = this;
}

MainWindow::~MainWindow()
{
    delete ui;
}

/*************************************************************************串口打开刷新*****/
void MainWindow::on_comBUTTON_clicked()
{
    if(uartModule.uartGetOpenStatus())
    {
        uartModule.uartClosePort();
        ui->comboBox->setEnabled(true);
        ui->comBUTTON->setText("打开串口");
    }
    else
    {
        if(uartModule.uartOpenPort(ui->comboBox->currentText()))
        {
            ui->comboBox->setEnabled(false);
            ui->comBUTTON->setText("关闭串口");
        }
    }
}

void MainWindow::on_refreshButton_clicked()
{
    ui->comboBox->clear();
    uartModule.uartRefreshCOM();
    ui->comboBox->addItems(uartModule.uartComAvailable);
}
/****************************************************************************************/


/***********************************************************************************接收***/
void MainWindow::TimerTimeout()
{
    uint32_t len;
    uint8_t tmp_buf[1024];
    len = uartModule.uartReadBuff(tmp_buf);
    tmp_buf[len] = '\0';
    if(len > 0)
    {
        if(ui->encryptBox->isChecked() == true)
        {
            _bProtocolDecrypt(tmp_buf, len);
        }
        if(0 > bProtocolParse(Protocol_n, tmp_buf, len))
        {
            QString str = QString::fromStdString((char *)tmp_buf);
            ui->recTEXT->append(str);
        }
    }
    quartTimer->start(100);
}

/****************************************************************************************/
QString hexToString(unsigned char *in, int len)
{
    int i;
    unsigned char inChar,hi,lo;
    QString s;

    for (i=0; i<len; i++)
    {
        inChar = in[i];

        hi = (inChar & 0xF0) >> 4;
        if (hi > 9)
            hi = 'A' + (hi - 0x0A);
        else
            hi += 0x30;
        s.append(hi);

        lo = inChar & 0x0F;
        if (lo > 9)
            lo = 'A' + (lo - 0x0A);
        else
            lo += 0x30;
        s.append(lo);

        s.append(0x20);
    }
    return s;
}

int MainWindow::ShowLog(bProtoID_t id, uint8_t cmd, uint8_t *param, bProtoLen_t param_len)
{
    QString str = "r->";
    str = str + "id: " + QString::number(id);
    str = str + "cmd: " + QString::number(cmd);
    str = str + "param: " + hexToString(param, param_len);
    ui->recTEXT->append(str);
    return 0;
}


int Dispatch(bProtoID_t id, uint8_t cmd, uint8_t *param, bProtoLen_t param_len)
{
    tmpClass->ShowLog(id, cmd, param, param_len);
    return 0;
}



void MainWindow::on_clearButton_clicked()
{
    ui->recTEXT->clear();
}

void MainWindow::on_pushButton_clicked()
{
    uint8_t table[128];
    int len;
    uint8_t param[7] = "BabyOS";
    len = bProtocolPack(Protocol_n, 0x520, 0x1, param, 7, table);
    if(ui->encryptBox->isChecked() == true)
    {
        _bProtocolEncrypt(table, len);
    }
    uartModule.uartSendBuff(table, len);
}
